package com.me.test.service.impl;

import com.me.test.mapper.merchantMapper;
import com.me.test.pojo.merchant;
import com.me.test.service.merchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class merchantServiceImpl implements merchantService {

    @Autowired
    private merchantMapper merchantMapper;

    @Override
    public List<merchant> getAllMerchants() {
        return merchantMapper.selectAllMerchants();
    }

    @Override
    public merchant getMerchantById(Integer id) {
        if (id == null) {
            return null;
        }
        return merchantMapper.selectMerchantById(id);
    }

    @Override
    public merchant getMerchantByMobile(String mobile) {
        if (mobile == null || mobile.trim().isEmpty()) {
            return null;
        }
        return merchantMapper.selectMerchantByMobile(mobile);
    }

    @Override
    public List<merchant> getMerchantsByStatus(String status) {
        if (status == null || status.trim().isEmpty()) {
            return null;
        }
        return merchantMapper.selectMerchantsByStatus(status);
    }

    @Override
    public List<merchant> getMerchantsWithPagination(int page, int size) {
        if (page < 1) {
            page = 1;
        }
        if (size < 1) {
            size = 10;
        }
        int offset = (page - 1) * size;
        return merchantMapper.selectMerchantsWithPagination(offset, size);
    }

    @Override
    public boolean addMerchant(merchant merchant) {
        if (merchant == null) {
            return false;
        }
        int result = merchantMapper.insertMerchant(merchant);
        return result > 0;
    }

    @Override
    public boolean updateMerchant(merchant merchant) {
        if (merchant == null || merchant.getId() == null) {
            return false;
        }
        int result = merchantMapper.updateMerchant(merchant);
        return result > 0;
    }

    @Override
    public boolean deleteMerchantById(Integer id) {
        if (id == null) {
            return false;
        }
        int result = merchantMapper.deleteMerchantById(id);
        return result > 0;
    }
}
